/**
 * @Title:  AuthMechanismActivity.java 
 * @Copyright:  XXX Co., Ltd. Copyright YYYY-YYYY,  All rights reserved 
 * @Description:  TODO<请描述此文件是做什么的> 
 * @author:  MeiYi 
 * @data:  2016-1-5 上午11:24:06 
 * @version:  V1.0 
 */

package artid.com.arttogo.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import artid.com.arttogo.R;
import artid.com.arttogo.activity.AucOrgDetActivity;
import artid.com.arttogo.adapter.AucOrgListAdapter;
import artid.com.arttogo.bean.JigouList;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.view.pulllist.PullToRefreshLayout;
import artid.com.arttogo.view.pulllist.PullableListView;

/**
 * 拍卖机构列表
 * 
 * @author 熊彬
 * @data: 2016-1-5 上午11:24:06
 * @version: V1.0
 */
public class AucOrgListActivity extends BaseFragment {
	private View mView;
	private Context mContext;
//	private PullableScrollView scrollView;
	String id;
	private ImageLoader mImageLoader;
	private AucOrgListAdapter adapter;
	PullToRefreshLayout mPullLayout;
	int pageIndex = 1,pagesize = 10;
	PullableListView mListView;
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sigmatrix.artworkart.BaseActivity#onCreate(android.os.Bundle)
	 */
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		Bundle args = getArguments();
		id = args != null ? args.getString("id") : "";
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
							 Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.activity_auc_organization_list, null);
		mContext = getActivity();
//		RequestQueue mQueue = Volley.newRequestQueue(mContext);
//		mImageLoader = new ImageLoader(mQueue, new BitmapCache());
		adapter = new AucOrgListAdapter(mContext);
		mListView = (PullableListView)mView.findViewById(R.id.lot_auc_org_list);
//		scrollView = (PullableScrollView)  mView.findViewById(R.id.sv_artdyn);
		mPullLayout = (PullToRefreshLayout) mView.findViewById(R.id.refresh_view);
		mPullLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
				@Override
				public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
					// TODO Auto-generated method stub
					pageIndex = 1;
					reqList();
//					reqAlldata();
				}
				
				@Override
				public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
					// TODO Auto-generated method stub
//					pullToRefreshLayout.setBottomDismiss();
					pageIndex++;
					reqList();
				}
			});
		reqList();
		return mView;
	}
	

	
	ArrayList<String> ids;
	void reqList() {
		VolleyBasic.getInstance(getActivity()).post(
				Cache.jigouList,
				VolleyMap.getInstance().jigouList(
						"1",
						String.valueOf(pageIndex), String.valueOf(pagesize)),
				JigouList.class, new VolleyCallback() {

					@Override
					public void onSuccess(final VolleyData vd) {
						// TODO Auto-generated method stub
						mPullLayout.loadmoreFinish(mPullLayout.SUCCEED);
						JSONObject jsonString = vd.getObj();
						JSONArray jsonArray = jsonString.optJSONArray("data");
						final List<JigouList> list = (List<JigouList>) vd
								.getList();
						if (list.isEmpty()) {
							// mViewBlank.setVisibility(View.VISIBLE);
							return;
						}
						// mViewBlank.setVisibility(View.GONE);
						if (pageIndex != 1) {
							// 下拉加载，添加数据
							for (int i = 0; i < list.size(); i++) {
								ids.add(list.get(i).getId());
							}
							adapter.addData(list);
							mListView
									.setOnItemClickListener(new OnItemClickListener() {

										@Override
										public void onItemClick(
												AdapterView<?> parent,
												View view, int position, long id) {
											Intent intent = new Intent(
													mContext,
													AucOrgDetActivity.class);
//											intent.putExtra("isOrg", true);
											// 得到新加的item，加入原来的json
											
											intent.putExtra("id", adapter.list.get(position).getId());
											startActivity(intent);

										}
									});
							return;
						}
						ids = new ArrayList<String>();
						for (int i = 0; i < list.size(); i++) {
							ids.add(list.get(i).getId());
						}
						adapter.setData(list);
						mListView.setAdapter(adapter);
						mListView
						.setOnItemClickListener(new OnItemClickListener() {

							@Override
							public void onItemClick(
									AdapterView<?> parent,
									View view, int position, long id) {
								Intent intent = new Intent(
										mContext,
										AucOrgDetActivity.class);
//								intent.putExtra("isOrg", true);
								// 得到新加的item，加入原来的json
								
								intent.putExtra("id", adapter.list.get(position).getId());
								
								startActivity(intent);

							}
						});
//						scrollView.fullScroll(ScrollView.FOCUS_UP);

					}

					@Override
					public void onError(VolleyError volleyError) {
						// TODO Auto-generated method stub
						mPullLayout.loadmoreFinish(mPullLayout.FAIL);
					}
				});
	}

	

	


}
